import urllib.request
import json
import os

# 项目路径配置
BASE_PATH = "./zhaopin_system/data/"
os.makedirs(BASE_PATH, exist_ok=True)


def crawl_zhaopin_job(page=1, keyword="java开发", city="801"):
    """
    爬取智联招聘职位数据
    :param page: 页码
    :param keyword: 搜索关键词
    :param city: 城市代码
    """
    url = "https://fe-api.zhaopin.com/c/i/search/positions"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0",
        "Referer": "https://www.zhaopin.com/",
        "Content-Type": "application/json"
    }
    data = {
        "S_SOU_FULL_INDEX": keyword,
        "S_SOU_WORK_CITY": city,
        "anonymous": 1,
        "eventScenario": "pcSearchedSouSearch",
        "order": 4,  # 按相关性排序
        "pageIndex": page,
        "pageSize": 20  # 每页20条数据
    }
    # 数据编码为JSON格式
    data_json = json.dumps(data).encode("utf-8")
    request = urllib.request.Request(
        url=url,
        headers=headers,
        data=data_json,
        method="POST"
    )
    try:
        response = urllib.request.urlopen(request)
        if response.getcode() == 200:
            content = response.read().decode("utf-8")
            # 保存原始数据到文件
            file_path = f"{BASE_PATH}raw_data_page{page}.json"
            with open(file_path, "w", encoding="utf-8") as f:
                f.write(content)
            print(f"[SUCCESS] 爬取第{page}页数据成功，保存至：{file_path}")
            return content
        else:
            print(f"[ERROR] 请求失败，状态码：{response.getcode()}")
            return None
    except Exception as e:
        print(f"[ERROR] 爬取异常：{str(e)}")
        return None


# 示例调用：爬取前3页数据
if __name__ == "__main__":
    # 获取用户输入
    try:
        max_page = int(input("请输入要爬取的总页数（例如3）："))
        keyword = input("请输入搜索关键词（例如java开发）：")
        city = input("请输入城市代码（例如801代表成都）：")

        for p in range(1, max_page + 1):
            crawl_zhaopin_job(page=p, keyword=keyword, city=city)
    except ValueError:
        print("输入的页码必须是整数！")
    except Exception as e:
        print(f"执行过程中发生错误：{str(e)}")